package per.wxp.kafka.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import per.wxp.kafka.service.FileReadService;
import per.wxp.kafka.util.Constant;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

@Component
public class ScheduledTasks {



    private Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
    private int fixedDelayCount = 1;
    private int fixedRateCount = 1;
    private int initialDelayCount = 1;
    private int cronCount = 1;


    final File tmpLogFile = new File("/home/wxp/tmp/test.log");
    final String msgInfo = "王鑫鹏--test !";

    final String path="/home/wxp/tmp/test.log";

    @Resource
    private FileReadService fileReadService;

    // 模拟  定时 写入 日志文件
    //fixedDelay = 5000表示当前方法执行完毕5000ms后，Spring scheduling会再次调用该方法
    @Scheduled(fixedDelay = 5000)
    public void testWriteLogs() {
        logger.info("===fixedDelay: 第{}次执行方法", fixedDelayCount++);
        try {
            fileReadService.writeLogs(tmpLogFile,msgInfo);
        } catch (IOException e) {
            logger.error("file write error ！");
            e.printStackTrace();
        }
    }
    public static long pointer = 0;


    @Scheduled(fixedDelay = 5000)
    public void testReadLogs() {
        logger.info("===fixedDelay: 第{}次执行方法", fixedDelayCount++);
        fileReadService.randomRed(path);
    }



}
